package com.cheea.associationBss.controller;

import java.util.List;

import com.cheea.associationBss.model.User;
import com.cheea.associationBss.util.MD5Util;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class UserController extends Controller{
	public void login(){
		render("login.jsp");
	}
	public void register(){
		render("register.jsp");
	}
	public void registerSt(){
		render("registerSt.jsp");
	}
	public void runRegis(){
		User user = getModel(User.class);
		String uname = getPara("uname").trim();
		String pwd = getPara("pwd").trim();
		
		user.set("uname", uname);
		user.set("pwd", MD5Util.getMD5(pwd));
		user.set("createTime", new java.sql.Timestamp( new java.util.Date().getTime()) );
		user.set("role", 3);
		user.set("nickname", getPara("nickname").trim());
		boolean r = false;
		try{
			r = user.save();
			User use= user.findFirst("select id from user u where u.uname='"+uname+"'");
			if(r){
				setAttr("msg", "恭喜哈，注册成功了,现在为您跳转到首页");
				setSessionAttr("uname", uname);
				setSessionAttr("uid", use.get("id"));
				setSessionAttr("role", 3);
				setAttr("redirectPage", "/associationBss");
			}else{
				setAttr("msg", "糟糕，注册失败了，现在为您跳转回注册页");
				setAttr("redirectPage", "/user/register");
			}
		}catch(Exception e){
			e.printStackTrace();
			String exceptionMsg=e.getMessage();
			if(exceptionMsg.contains("uname") && exceptionMsg.contains("Duplicate")){
				setAttr("msg", "糟糕，用户名："+uname+" 被别人抢啦，换一个试试吧，现在为您跳转回注册页");
				setAttr("redirectPage", "/user/register");
			}
		}
		render("/view/common/msg.jsp");
	}
	public void runRegisSt(){
		User user = getModel(User.class);
		String uname = getPara("uname").trim();
		String pwd = getPara("pwd").trim();
		
		user.set("uname", uname);
		user.set("pwd", MD5Util.getMD5(pwd));
		user.set("createTime", new java.sql.Timestamp( new java.util.Date().getTime()) );
		user.set("role", 2);
		user.set("associationName", getPara("associationName").trim());
		boolean r = false;
		try{
			r = user.save();
			User use= user.findFirst("select id from user u where u.uname='"+uname+"'");
			if(r){
				setAttr("msg", "恭喜哈，注册成功了,现在为您跳转到首页");
				setSessionAttr("uname", uname);
				setSessionAttr("uid", use.get("id"));
				setSessionAttr("role", 2);
				setAttr("redirectPage", "/associationBss");
			}else{
				setAttr("msg", "糟糕，注册失败了，现在为您跳转回注册页");
				setAttr("redirectPage", "/user/register");
			}
		}catch(Exception e){
			e.printStackTrace();
			String exceptionMsg=e.getMessage();
			if(exceptionMsg.contains("uname") && exceptionMsg.contains("Duplicate")){
				setAttr("msg", "糟糕，用户名："+uname+" 被别人抢啦，换一个试试吧，现在为您跳转回注册页");
				setAttr("redirectPage", "/user/register");
			}
		}
		render("/view/common/msg.jsp");
	}
	public void runLogin(){
		String uname = getPara("uname").trim();
		String pwd = getPara("pwd").trim();
		User user = User.dao.login(uname, MD5Util.getMD5(pwd));
	    if(user!=null) {
	    	setAttr("msg", "登录成功,现在为您跳转到首页");
			setSessionAttr("uname", uname);
			setSessionAttr("uid",user.get("id"));
			setSessionAttr("role",user.get("role"));
			setAttr("redirectPage", "/associationBss");
	    }else{
	    	setAttr("msg", "登录失败,现在为您跳转到登录页");
			setAttr("redirectPage", "/associationBss");
	    }
	    render("/view/common/msg.jsp");
	}
	public void logout(){
		getSession().removeAttribute("uname");
		getSession().removeAttribute("uid");
		setAttr("msg", "您已退出登录,现在为您跳转到首页");
		setAttr("redirectPage", "/associationBss");
		render("/view/common/msg.jsp");
	}
	public void getUserListAll(){
		User user = getModel(User.class);
		List<User> userList= user.queryListByAll();
		//renderJson(userList);
		renderJson("aaData", userList);
	}
	
	public void getUserListById(){
		int id = getParaToInt("id");
		User user = getModel(User.class);
		List<User> userList= user.queryListById(id);
		//renderJson(userList);
		renderJson("aaData", userList);
	}
	
	public void delById(){
		int id = getParaToInt("id");
		User user = getModel(User.class);
		boolean flag=user.deleteById(id);
		//renderJson(userList);
		renderJson(flag);
	}
	
	public void delByStAdmin(){
		int id = getParaToInt("id");
		Record user = Db.findById("user", id).set("belongs", null);
		Db.update("user",user);
		renderJson(true);
	}
	
	
	public void addUserByAdmin(){
		User user = getModel(User.class);
		String uname = getPara("uname").trim();
		String pwd = getPara("pwd").trim();
		String email = getPara("email").trim();
		String isFlag = getPara("isFlag").trim();
		String nickname = getPara("nickname").trim();
		int role=3;
		if("1".equals(isFlag)){
			role=2;
		}
		
		user.set("uname", uname);
		user.set("pwd", MD5Util.getMD5(pwd));
		user.set("email", email);
		user.set("createTime", new java.sql.Timestamp( new java.util.Date().getTime()) );
		user.set("role", role);
		user.set("nickname", nickname);
		boolean r = false;
		try{
			r = user.save();
			if(r){
				renderJson(1);
			}else{
				renderJson(0);
			}
		}catch(Exception e){
			e.printStackTrace();
			String exceptionMsg=e.getMessage();
			if(exceptionMsg.contains("uname") && exceptionMsg.contains("Duplicate")){
				//setAttr("msg", "糟糕，用户名："+uname+" 被别人抢啦，换一个试试吧，现在为您跳转回注册页");
				//setAttr("redirectPage", "/user/register");
				renderJson(3);
			}
		}
	}
	
	public void upUserByAdmin(){
		User user = getModel(User.class);
		String uname = getPara("uname").trim();
		String pwd = getPara("pwd").trim();
		String email = getPara("email").trim();
		String isFlag = getPara("isFlag").trim();
		String nickname = getPara("nickname").trim();
		String id = getPara("id").trim();
		int role=3;
		if("1".equals(isFlag)){
			role=2;
		}
		user.set("id", Integer.parseInt(id));
		user.set("uname", uname);
		user.set("pwd", MD5Util.getMD5(pwd));
		user.set("email", email);
		user.set("createTime", new java.sql.Timestamp( new java.util.Date().getTime()) );
		user.set("role", role);
		user.set("nickname", nickname);
		boolean r = false;
		try{
			r = user.update();
			if(r){
				renderJson(1);
			}else{
				renderJson(0);
			}
		}catch(Exception e){
			e.printStackTrace();
			String exceptionMsg=e.getMessage();
			if(exceptionMsg.contains("uname") && exceptionMsg.contains("Duplicate")){
				//setAttr("msg", "糟糕，用户名："+uname+" 被别人抢啦，换一个试试吧，现在为您跳转回注册页");
				//setAttr("redirectPage", "/user/register");
				renderJson(3);
			}
		}
	}
	
	public void upUserToSt(){
		Record user = Db.findById("user", getParaToInt("id")).set("belongs", getParaToInt("uid"));
		Db.update("user",user);
		renderJson(1);
	}
	
}
